const { resolve } = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin

const ANA = true

const PLUGIN_BY_ENV = []

if(ANA) {
    PLUGIN_BY_ENV.push(new BundleAnalyzerPlugin())
}

module.exports = {
    mode: 'development',

    entry: resolve(__dirname, 'src/index.js'),

    output: {
        path: resolve(__dirname, 'dist'),
        filename: '[name].[hash:8].js'
    },

    devServer: {
        port: 3001
    },

    cache: {
        type: 'filesystem'
    },

    module: {
        rules: [
            {
                test: /.m?js$/,
                exclude: /(node_modules)/,
                use: 'swc-loader'
            },
            {
                test: /.js$/,
                exclude: /(node_modules)/,
                use: [
                    {
                        loader: 'thread-loader',
                        options: {
                            workers: 8
                        }
                    },
                    {
                        loader: 'babel-loader',
                        options: {
                            presets: ['@babel/preset-env']
                        }
                    }
                ]
            }
        ]
    },

    plugins: [
        new HtmlWebpackPlugin(),
        ...PLUGIN_BY_ENV
    ]
}